
import { messageConfig } from './config'
import { deepAssign } from '@/utils/tools/deepAssign'
/**
 * 绘制图形shap 类，
 * 配置项参考config.js
 * 样式项参考style.js
 */
export default class OlMessage {
  constructor (_config) {
    let typeArr = ['success', 'warning', 'info', 'error']
    this.options = deepAssign(messageConfig, _config) //  配置项
    this.messageEle = null
    console.log(this.options.type, typeArr.indexOf(this.options.type.toString()))
    if (typeArr.indexOf(this.options.type.toString()) > -1) {
      this.onCreateMessageEle()
    } else {
      this.onCreateMessageEle(true)
    }
  }

  onCreateMessageEle (bool = false) {
    this.messageEle = document.createElement('div')
    this.messageEle.id = 'ol-message'
    if (!bool) {
      this.messageEle.className = `ol-message ${this.options.center ? 'center' : ''} ${this.options.type} ${this.options.customClass}`
      this.messageEle.innerText = this.options.message || '消息提示'
    } else {
      this.messageEle.className = 'ol-message warning'
      this.messageEle.innerText = '消息框没有该类型，只有：success, warning, info, error'
    }

    document.body.appendChild(this.messageEle)
    if (this.options.duration < 0 || !this.options.duration) {
      document.body.removeChild(this.messageEle)
    } else {
      setTimeout(() => {
        document.body.removeChild(this.messageEle)
      }, this.options.duration)
    }
  }
}
